import {
  getRescenterToken,
  getChanganUserByToken,
  setRescenterTokenByHandle,
} from "@/api/user/index";
import { mockUserBaseConfig } from "@/config/env-config";
import { setChanganToken } from "./token-util";
import { setEmployeeNo } from "./common-storage";
import { getUser } from "@/cordova/user";
import { setUsername } from "@/utils/base/common-storage";

const config = process.env.CONFIG as any;

console.log("----process.env.CONFIG in changan info util----", config.env);

export async function initChanganInfo() {
  let userInfo = null;

  switch (config.env) {
    case "local":
      // await getRescenterToken();
      await setRescenterTokenByHandle();
      userInfo = await getChanganUserByToken();
      setEmployeeNo(userInfo.loginID);
      break;
    case "mock":
      console.log("in the mock");
      setChanganToken(mockUserBaseConfig.token);
      console.log("mock, employeenumber", mockUserBaseConfig.employeenumber);
      setEmployeeNo(mockUserBaseConfig.employeenumber);
      break;
    default:
      userInfo = await getUser();
      console.log("----userInfo----", userInfo);
      setChanganToken(userInfo.extraToken);
      setEmployeeNo(userInfo.employeenumber);
      setUsername(userInfo.cn);
  }
}
